package com.itany.shop.controller;

import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserExistException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;
import com.itany.shop.service.UserService;

import java.util.List;
import java.util.Scanner;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class StartController {
    private UserService userService = (UserService) ObjectFactory.getObject("userService");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");
    private Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new StartController().showMenu();
    }

    public void showMenu() {
        System.out.println("**************************欢迎来到紫金商城**************************");
        System.out.print("请选择服务(1.注册 2.登录 3.查看商品信息 4.分页查询 5.退出):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            case 3:
                findProducts();
                break;
            case 4:
                findPage();
                break;
            case 5:
                System.out.println("提示:您已经退出");
                break;
            default:
                System.out.println("错误:对不起,您输入的选项有误,请重新输入!");
                break;
        }
    }

    public void register() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        System.out.print("请输入电话号码:");
        String phone = sc.next();
        System.out.print("请输入地址:");
        String address = sc.next();
        try {
            //通过 userService.register()方法把疏浚传入数据库
            userService.register(username, password, phone, address);
            System.out.println("提示:注册成功!");
        } catch (RequestParameterErrorException e) {
            System.out.println("注册失败，失败原因:" + e.getMessage());
        } catch (UserExistException e) {
            System.out.println("注册失败，失败原因:" + e.getMessage());
        }
        showMenu();
    }

    public void login() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        try {
            User user = userService.login(username, password);
            new MainController(user).showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("提示，登陆失败，失败原因:" + e.getMessage());
            showMenu();
        } catch (UserNotExistException e) {
            System.out.println("提示，登陆失败，失败原因:" + e.getMessage());
            showMenu();
        }
        showMenu();

    }

    public void findProducts() {
        List<Product> products = productService.findAll();
        System.out.println("商品编号\t\t" + "商品名称\t\t" + "商品价格");
        for (Product product : products) {
            System.out.println(product.getId() + "\t\t" + product.getName() + "\t\t" + product.getPrice());
        }
        showMenu();
    }


    public void findPage() {
        System.out.print("请输入查询从第几页开始的:");
        String pageNo = sc.next();
        System.out.print("请输入一页有几条:");
        String pageSize = sc.next();
        try {
            List<Product> products = productService.findPage(pageNo, pageSize);
            System.out.println("商品编号\t\t商品名称\t\t商品单价");
            for (Product product : products) {
                System.out.println(product.getId() + "\t\t" + product.getName() + "\t\t" + product.getPrice());
            }
        } catch (ProductNotExistException e) {
            throw new RuntimeException("错误，原因:" + e.getMessage());
        }
        showMenu();
    }
}
